package com.hm.shopping.web.tourists;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.hm.common.annotation.WebClientRequestIntercept;
import com.hm.common.su.bean.PageInfo;
import com.hm.common.util.CommonUtil;
import com.hm.common.util.EncryptUtil.Base64;
import com.hm.common.util.StringUtil;
import com.hm.shopping.auto.SystemEnvConfigProperties;
import com.hm.shopping.auto.su.BaseController;
import com.hm.shopping.auto.su.R.Uri;
import com.hm.shopping.auto.su.helper.HmSessionFactory;
import com.hm.shopping.service.ClassificationService;
import com.hm.shopping.service.GoodsIntroductionService;
import com.hm.shopping.service.ShopcartService;
import com.hm.shopping.vo.ClassificationDetailVo;
import com.hm.shopping.vo.GoodsIntroductionVo;
import com.hm.shopping.vo.su.GoodsAttributeVo;

/**
 * @author shishun.wang
 * @date 2018年1月19日 下午4:13:34
 * @version 1.0
 * @describe
 */
@Controller
@RequestMapping(Uri.PC + "/search")
public class SearchController extends BaseController {
	
	@Autowired
	private ShopcartService shopcartService;
	
	@Autowired
	private ClassificationService classificationService;

	@Autowired
	private GoodsIntroductionService goodsIntroductionService;

	@Autowired
	private SystemEnvConfigProperties systemEnvConfigProperties;

	@WebClientRequestIntercept(auth = false)
	@RequestMapping(value = "goods.do", method = RequestMethod.GET)
	public String search(Model model, String goodsName, String cdcode,String ccode, @RequestParam(value = "pageNum", required = false, defaultValue = "1") int pageNum) {
		List<GoodsAttributeVo> attributes = new ArrayList<GoodsAttributeVo>();
		if(!StringUtil.isBlankOrNull(cdcode)) {
			ClassificationDetailVo classificationDetailVo = classificationService.getClassificationDetailByCode(cdcode);
			if(CommonUtil.isNotEmpty(classificationDetailVo)) {
				model.addAttribute("classificationDetailName", classificationDetailVo.getClassificationDetailName());// 商品分类明细名称
				attributes = classificationDetailVo.getAttributes();
			}
		}
		model.addAttribute("classificationDetailAttributes", attributes);// 商品分类明细属性搜索条件
		List<ClassificationDetailVo> relatedGoods = classificationService.getClassificationDetailsByCcode(ccode);
		model.addAttribute("relatedGoods", CommonUtil.isEmpty(relatedGoods)?null:relatedGoods);//相关产品推荐
		
		PageInfo<GoodsIntroductionVo> paggingGoodsIntroductions = goodsIntroductionService.paggingGoodsIntroductions(goodsName, cdcode, new PageInfo.PageInfoParam(pageNum, 10));
		model.addAttribute("goodsIntroductions", paggingGoodsIntroductions.getContent());// 商品列表信息
		model.addAttribute("searchGoodsIntroductionTotalSize", paggingGoodsIntroductions.getTotalSize());// 搜索到商品数量
		model.addAttribute("classicalCollocation", goodsIntroductionService.goodsClassicalCollocationList(goodsName, cdcode));// 经典搭配
		model.addAttribute("copyrightStatement", new String(Base64.decode(systemEnvConfigProperties.getFooterCopyrightStatementPC().toCharArray())));// 版权信息
		try {
			model.addAttribute("shopcartGoodsCount", shopcartService.getShopcartGoodsCount(HmSessionFactory.currentUserIdString()));
		}catch(Exception e) {
			model.addAttribute("shopcartGoodsCount", shopcartService.getShopcartGoodsCount(null));
		}
		return "search";
	}
}
